package com.zhiyou.util;

import com.zhiyou.entity.Column;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sanshi
 * @desc
 * @create 2021-09-13 20:45
 */
public class ColumnUtil extends DBUtil {
    public List<Column> getAllColumn(){
        ArrayList<Column> list = new ArrayList<Column>();
        getConn();
        String sql = "select * from news_chnl";
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Column column = new Column(rs.getString("id"), rs.getString("name"));
                list.add(column);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        closeConn();
        return list;
    }
    //通过columname获取column对象
    public Column getColumnByCname(String columname){
        Column column = null;
        getConn();
        String sql = "select * from news_chnl where name = ?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,columname);
            rs = ps.executeQuery();
            if (rs.next()){
                column = new Column(rs.getString("id"), rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        closeConn();
        return column;
    }
}
